﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Data;
using emp.SqlServerDAL;
using System.Data.SqlClient;
using emp.Model.SysManage;

public partial class crm_taste_UpMember : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            //意向车型 
            string sql = "SELECT  QSA_UID ,QSA_QSD_UID,QSA_NUMBER,isnull(QSA_ANSWER_NAME,'') as QSA_ANSWER_NAME ,isnull(QSA_ANSWER_VALUE,''),QSA_STATUS FROM QUEST_STD_ANSWER   where QSA_QSD_UID ='190' order by  QSA_ANSWER_NAME";
            System.Data.DataTable dt1 = getTableInfo(sql);
            this.TCI_CAR_MODEL.Items.Clear();
           // this.TCI_CAR_MODEL.Items.Add(new ListItem("请选择", "请选择"));
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                this.TCI_CAR_MODEL.Items.Add(new ListItem(dt1.Rows[i]["QSA_ANSWER_NAME"].ToString(), dt1.Rows[i]["QSA_ANSWER_NAME"].ToString()));
            }
            //购车计划时间
            /*
            sql = "SELECT  QSA_UID ,QSA_QSD_UID,QSA_NUMBER,isnull(QSA_ANSWER_NAME,'') as QSA_ANSWER_NAME ,isnull(QSA_ANSWER_VALUE,''),QSA_STATUS FROM QUEST_STD_ANSWER   where QSA_QSD_UID ='35' order by  QSA_ANSWER_NAME";
            dt1 = getTableInfo(sql);
            this.TCI_PLAN_TIME.Items.Clear();
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                this.TCI_PLAN_TIME.Items.Add(new ListItem(dt1.Rows[i]["QSA_ANSWER_NAME"].ToString(), dt1.Rows[i]["QSA_ANSWER_NAME"].ToString()));

            }
            */
            //购车预算
            /*
            sql = "SELECT  QSA_UID ,QSA_QSD_UID,QSA_NUMBER,isnull(QSA_ANSWER_NAME,'') as QSA_ANSWER_NAME ,isnull(QSA_ANSWER_VALUE,''),QSA_STATUS FROM QUEST_STD_ANSWER   where QSA_QSD_UID ='36' order by  QSA_ANSWER_NAME";
            dt1 = getTableInfo(sql);
            this.TCI_BUDGET_PUR.Items.Clear();
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                this.TCI_BUDGET_PUR.Items.Add(new ListItem(dt1.Rows[i]["QSA_ANSWER_NAME"].ToString(), dt1.Rows[i]["QSA_ANSWER_NAME"].ToString()));

            }
             */
            //所在省
            sql = "select qsa_answer_name from QUEST_STD_ANSWER  where qsa_uid in (select qsrd_qsa_uid_parent from QUEST_STD_RELATE_DETAIL where qsrd_qsr_uid='1'   )";
            dt1 = getTableInfo(sql);
            this.TCI_PROVINCE.Items.Clear();
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                this.TCI_PROVINCE.Items.Add(new ListItem(dt1.Rows[i]["qsa_answer_name"].ToString(), dt1.Rows[i]["qsa_answer_name"].ToString()));

            }



            //现有车品牌
            sql = "select qsa_answer_name from QUEST_STD_ANSWER  where qsa_uid in (select qsrd_qsa_uid_parent from QUEST_STD_RELATE_DETAIL where qsrd_qsr_uid='2'   )";
            dt1 = getTableInfo(sql);
            this.TCI_BRAND_OWNED.Items.Clear();
            this.TCI_BRAND_OWNED.Items.Add(new ListItem("请选择", "请选择"));
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                this.TCI_BRAND_OWNED.Items.Add(new ListItem(dt1.Rows[i]["qsa_answer_name"].ToString(), dt1.Rows[i]["qsa_answer_name"].ToString()));

            }


            TCI_PROVINCE_SelectedIndexChanged(null,null);
            TCI_CITY_SelectedIndexChanged(null, null);
            TCI_BRAND_OWNED_SelectedIndexChanged(null, null);


        }
    }
    private DataTable getTableInfo(string sql)
    {
        DataSet data = new DataSet();
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            try
            {
                sda.SelectCommand = new SqlCommand();
                sda.SelectCommand.CommandText = sql;
                sda.SelectCommand.Connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC);
                sda.Fill(data);
            }
            catch { }
        }
        return data.Tables[0];
    }


    private bool ExecSql(string sql)
    {
        bool ret = false;
        using (SqlConnection conn = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
        {
            conn.Open();
            using (SqlTransaction trans = conn.BeginTransaction())
            {
                try
                {
                    SQLHelper.ExecuteNonQuery(trans, CommandType.Text, sql, null);
                    trans.Commit();
                    ret = true;
                }
                catch (SqlException err)
                {
                    trans.Rollback();
                }
            }
        }
        return ret;
    }
    protected void recBtn_Click(object sender, EventArgs e)
    {

        result.Text = "";
        if (!getYZ())
        {
            return;
        }

        string[] str = new string[15];

        str[0] = TCI_NAME.Text;
        if (TCI_GANDER_1.Checked)
        {
            str[1] = "M";
        }
        else 
        {
            str[1] = "F";
        }
        

        str[2] = TCI_TELEPHONE.Text;
        if (TCI_CAR_MODEL.SelectedValue == "请选择")
        {
            str[3] = "";
        }
        else 
        {
            str[3] = TCI_CAR_MODEL.Text;
        }
        
        str[4] = TCI_PLAN_TIME.Text;
        str[5] = TCI_BUDGET_PUR.Text;


        if (TCI_IS_CONTACT_1.Checked)
        {
            str[6] = "Y";
        }
        else
        {
            str[6] = "N";
        }
        if (TCI_IS_TEST_1.Checked)
        {
            str[7] = "Y";
        }
        else
        {
            str[7] = "N";
        }

        str[8] = TCI_PROVINCE.Text;
        str[9] = TCI_CITY.Text;
        str[10] = TCI_DEALER_INTENTION.Text;



        if (TCI_BRAND_OWNED.SelectedValue == "请选择")
        {
            str[11] = "";
        }
        else
        {
            str[11] = TCI_BRAND_OWNED.Text;
        }
        if (TCI_MODEL_OWNED.SelectedValue == "请选择")
        {
            str[12] = "";
        }
        else
        {
            str[12] = TCI_MODEL_OWNED.Text;
        }

        str[13] = TCI_NOTES.Text;
         if (TCI_IS_MANUAL_1.Checked)
        {
            str[14] = "需要";
        }
        else
        {
            str[14] = "不需要";
        }
         UserInfo myUserInfo = new UserInfo();
          if ((UserInfo)Session["USER_KEY"] != null)
          {
                myUserInfo = (UserInfo)Session["USER_KEY"];

          }
          string sql15 = " INSERT INTO TASTE_CAR_INFORMATION(TCI_NAME      ,TCI_GANDER      ,TCI_TELEPHONE      ,TCI_CAR_MODEL      ,TCI_PLAN_TIME      ,TCI_BUDGET_PUR      ,TCI_IS_CONTACT ,TCI_IS_TEST     ,TCI_PROVINCE      ,TCI_CITY      ,TCI_DEALER_INTENTION      ,TCI_BRAND_OWNED      ,TCI_MODEL_OWNED      ,TCI_NOTES      ,TCI_TCB_ID,TCI_UP_USER,TCI_DATE,TCI_IS_MANUAL)VALUES('" + str[0].ToString() + "','" + str[1].ToString() + "','" + str[2].ToString() + "','" + TextBox1.Text + "','" + str[4].ToString() + "','" + str[5].ToString() + "','" + str[6].ToString() + "','" + str[7].ToString() + "','" + str[8].ToString() + "','" + str[9].ToString() + "','" + str[10].ToString() + "','" + str[11].ToString() + "','" + str[12].ToString() + "','" + str[13].ToString() + "','0','" + myUserInfo.UserID + "',getdate(),'" + str[14].ToString() + "') ";
        bool bo1 = ExecSql(sql15);


        string sql = "select *from TASTE_CAR_INFORMATION where TCI_TELEPHONE='" + str[2] + "'";
        System.Data.DataTable dt1 = getTableInfo(sql);
        string tid = "0";
        if (dt1.Rows.Count > 0)
        {
            tid = dt1.Rows[0]["TCI_ID"].ToString();
        }
        if (bo1)
        {

            string sql151 = "exec p_impost_taste_car_one '" + tid + "'";
            bool bo111 = ExecSql(sql151);


            Response.Redirect("MemberList.aspx", true);
            //result.Text = "提交成功";
        }
        
        
    }



    private bool getYZ() 
    {
        bool bo=true;
        if (TCI_NAME.Text=="")
        {
            result.Text = "姓名不能为空";
            bo = false;
        }
        if (TCI_TELEPHONE.Text == "")
        {
            result.Text = "手机不能为空";
            bo = false;
        }
        else 
        {
            if (!IsMobile(TCI_TELEPHONE.Text))
            {
                result.Text = "手机格式有误";
                bo = false;
            }
            if (!IsDH(TCI_TELEPHONE.Text))
            {
                result.Text = "手机号码重复";
                bo = false;
            }


            
        }
        if (TCI_DEALER_INTENTION.SelectedValue == "")
        {
            result.Text = "经销商不能为空";
            bo = false;
        }


        


        return bo;
    }

    private bool IsDH(string str)
    {
        bool bo = true;
        string sql = "select *from TASTE_CAR_INFORMATION where TCI_TELEPHONE='" + str + "'";
        System.Data.DataTable dt1 = getTableInfo(sql);

        if (dt1.Rows.Count > 0)
        {
            bo = false;
        }

        return bo;
    }


    private bool IsMobile(string source)
    {
        return Regex.IsMatch(source, @"^1[123456789]\d{9}$", RegexOptions.IgnoreCase);
    }

    protected void TCI_PROVINCE_SelectedIndexChanged(object sender, EventArgs e)
    {




        string sql = "select qsrd_uid,(select qsa_answer_name from QUEST_STD_ANSWER  where qsa_uid=qsrd_qsa_uid_parent ) as pname  ,(select qsa_answer_name from QUEST_STD_ANSWER  where qsa_uid=qsrd_qsa_uid_child) as cname from QUEST_STD_RELATE_DETAIL  where qsrd_qsr_uid='1'   and qsrd_qsa_uid_parent in (select qsa_uid from QUEST_STD_ANSWER where qsa_answer_name='" + TCI_PROVINCE .SelectedValue+ "')";
        System.Data.DataTable dt1 = getTableInfo(sql);
        this.TCI_CITY.Items.Clear();
        for (int i = 0; i < dt1.Rows.Count; i++)
        {
            this.TCI_CITY.Items.Add(new ListItem(dt1.Rows[i]["cname"].ToString(), dt1.Rows[i]["cname"].ToString()));

        }

        TCI_CITY_SelectedIndexChanged(null,null);

    }
    protected void TCI_CITY_SelectedIndexChanged(object sender, EventArgs e)
    {
        string sql = "select qsrd_uid,(select qsa_answer_name from QUEST_STD_ANSWER  where qsa_uid=qsrd_qsa_uid_parent) as pname  ,(select qsa_answer_name from QUEST_STD_ANSWER  where qsa_uid=qsrd_qsa_uid_child) as cname from QUEST_STD_RELATE_DETAIL  where qsrd_qsr_uid='9' and qsrd_qsa_uid_parent in (select qsa_uid from QUEST_STD_ANSWER where qsa_answer_name='" + TCI_CITY .SelectedValue+ "') ";
        System.Data.DataTable dt1 = getTableInfo(sql);
        this.TCI_DEALER_INTENTION.Items.Clear();
        for (int i = 0; i < dt1.Rows.Count; i++)
        {
            //string sql1 = "select * from [SYSTEM_USER_TBL] where [user_name]='"+dt1.Rows[i]["cname"].ToString()+"'";
            //System.Data.DataTable dt11 = getTableInfo(sql1);


            this.TCI_DEALER_INTENTION.Items.Add(new ListItem(dt1.Rows[i]["cname"].ToString(), dt1.Rows[i]["cname"].ToString()));

        }
    }
    protected void TCI_BRAND_OWNED_SelectedIndexChanged(object sender, EventArgs e)
    {
        string sql = "select qsrd_uid,(select qsa_answer_name from QUEST_STD_ANSWER  where qsa_uid=qsrd_qsa_uid_parent) as pname  ,(select qsa_answer_name from QUEST_STD_ANSWER  where qsa_uid=qsrd_qsa_uid_child) as cname from QUEST_STD_RELATE_DETAIL  where qsrd_qsr_uid='2' and qsrd_qsa_uid_parent in (select qsa_uid from QUEST_STD_ANSWER where qsa_answer_name='" + TCI_BRAND_OWNED.SelectedValue + "') ";
        System.Data.DataTable dt1 = getTableInfo(sql);
        this.TCI_MODEL_OWNED.Items.Clear();
        this.TCI_MODEL_OWNED.Items.Add(new ListItem("请选择", "请选择"));
        for (int i = 0; i < dt1.Rows.Count; i++)
        {
            this.TCI_MODEL_OWNED.Items.Add(new ListItem(dt1.Rows[i]["cname"].ToString(), dt1.Rows[i]["cname"].ToString()));

        }
    }
    protected void recBtn0_Click(object sender, EventArgs e)
    {
        Response.Redirect("MemberList.aspx", true);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox1.Text = TextBox1.Text + TCI_CAR_MODEL.SelectedValue + ";";



    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
    }
}
